class TagsController < ApplicationController
  before_filter :login_required, :only => [ :edit, :update, :destroy, :harvest]
  require 'digest/sha1'
  require 'chart'
  
  # GET /tags
  # GET /tags.xml
  def index
    @tags = Tag.all
    @user = current_user

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tags }
    end
  end

  # GET /tags/1
  # GET /tags/1.xml
  def show
    @tag = Tag.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @tag }
    end
  end

  # GET /tags/1
  # GET /tags/1.xml
  def details
    @tag = Tag.find(params[:id])
  end

  # GET /tags/new
  # GET /tags/new.xml
  def new
    @tag = Tag.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tag }
    end
  end

  # GET /tags/1/edit
  def edit
    @tag = Tag.find(params[:id])
  end

  # POST /tags
  # POST /tags.xml
  def create
    params[:tag][:name] = params[:tag][:name].downcase
    if logged_in?
      if Tag.find(:first, :conditions => {:name => params[:tag][:name]})
        flash[:notice] = 'This tag allready exists. You have been granted viewing priveledges.'
        @found_tag = Tag.find(:first, :conditions => {:name => params[:tag][:name]})
        if !Priv.find(:first, :conditions => {:user_id => current_user, :tag_id => @found_tag})
          @priv = Priv.new(:user => current_user, :tag => @found_tag, :is_owner => false, :can_see => true, :can_harvest => false)
          @priv.save
        end
      else
        @tag = Tag.new(params[:tag])
        @priv = Priv.new(:user => current_user, :tag => @tag, :is_owner => true, :can_see => true, :can_harvest => true)
        flash[:notice] = 'Tag was successfully created.'
        @tag.save        
        @priv.save
      end
      redirect_to "#{ActionController::Base.relative_url_root}/"
    else
      if Tag.find(:first, :conditions => {:name => params[:tag][:name]})
        @id = Tag.find(:first, :conditions => {:name => params[:tag][:name]}).id
        redirect_to "#{ActionController::Base.relative_url_root}/tags/#{@id}"
      else
        flash[:notice] = 'That tag does not exist, if you wish to create it, please login.'
        redirect_to "#{ActionController::Base.relative_url_root}/"
      end
    end
  end
  
  # GET /tags/confirmation/1
  # GET /tags/1.xml
  def confirmation
    @tag = Tag.find(params[:id])
    
    respond_to do |format|
      format.html # confirmation.html.erb
      format.xml  { render :xml => @tag }
    end
  end

  # PUT /tags/1
  # PUT /tags/1.xml
  def update
    @tag = Tag.find(params[:id])

    respond_to do |format|
      if @tag.update_attributes(params[:tag])
        flash[:notice] = 'Tag was successfully updated.'
        format.html { redirect_to(@tag) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tag.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tags/1
  # DELETE /tags/1.xml
  def destroy
    @tag = Tag.find(params[:id])
    @tag.delete

    redirect_to "#{ActionController::Base.relative_url_root}/"
  end
  
  def harvest
    @tag = Tag.find(params[:id])
    @time = Time.new()
    if @tag.last_harvest == nil || (@tag.last_harvest.to_i + 43200) < @time.to_i
      @tag.update_attributes(:last_harvest => @time)
      flash[:notice] = Harvester.request_harvest(@tag)
    else
      flash[:notice] = 'Harvesting has allready taken place in the last 12 hours. Please try again later.'
    end
    redirect_to "#{ActionController::Base.relative_url_root}/"
  end
end
